var itmeHtml;
var _articleCount = 0;
$.fn.simpleTree = function(data,node,checked,title,bindMethod,mn){
	var dataImg = '<img src="'+node+'/images/tree/datbase.png" width="14px" height="14px"  align="bottom"/>';
	var elbow = '<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:20px;"/>';
	var elbow_end = '<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:20px;"/>';
	var ulHtml = '<ul class="ul_tree">';
	
	if(checked){
		ulHtml += '<li>'+dataImg+'<b style="font-size:13px;">'+title+'</b><b id="website_ArticleCont" style="font-size:13px;"></b></li>';
	}else{
		//onclick="queryAllArticleByWebSiteId(this)" 网站下所有文章
		ulHtml += '<li >'+dataImg+'<b style="font-size:13px;">'+title+'</b><b id="website_ArticleCont" style="font-size:13px;"></b></li>';
	}
	
	mn = (mn == undefined)?mn:'';
	bindMethod = (bindMethod == null)?'selectTree(this)':bindMethod;
	
	var arraylength = 0,arrLevel = checkLevel(data);
	$.each(data,function(i,j){
		if(j.tColumn.fcolumnlevel == arrLevel[0])
			arraylength ++;
	});

	$.each(data,function(i,u){				
			if(u.tColumn.fcolumnlevel == arrLevel[0]){//一级栏目
				arraylength -- ;
				var nodeSize = checkNode(data,u.tColumn.id);
				if(nodeSize > 0 && arraylength > 0){
					if(checked){
						ulHtml += '<li  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')"  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					itmeHtml = '';
					ulHtml += childNodeHtml(data,nodeSize,node,u.tColumn.id,false,checked,bindMethod,mn);
				}else if(nodeSize > 0 && arraylength == 0){
					if(checked){
						ulHtml += '<li  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')"  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					itmeHtml = '';
					ulHtml += childNodeHtml(data,nodeSize,node,u.tColumn.id,true,checked,bindMethod,mn);
				}else if(nodeSize == 0 && arraylength > 0){
					if(checked){
						ulHtml += '<li  value="'+u.tColumn.id+'">'+elbow+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')" value="'+u.tColumn.id+'">'+elbow+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}else if(nodeSize == 0 &&  arraylength == 0){
					if(checked){
						ulHtml += '<li value="'+u.tColumn.id+'">'+elbow_end+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')" value="'+u.tColumn.id+'">'+elbow_end+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}
				
			}	
	});
	ulHtml += '</ul>';
	return ulHtml;
};
//栏目级数
function checkLevel(data){
	var arr = [] , res = false;
	$.each(data,function(i,u){
		for(var tem = 0,len = arr.length; tem < len; tem ++){
			if(arr[tem] == u.tColumn.fcolumnlevel){
				res = true;
			}
		}
		if(!res){
			arr.push(u.tColumn.fcolumnlevel);
			res = false;
		}
	});
	return arr;
}
/////子栏目数量
function checkNode(data,tcId){
	var node = 0;
	$.each(data,function(i,u){
		if(u.tColumn.fcolumngp == tcId && u.tColumn.id != tcId){
			node ++;
		}
	});
	return node;
}

/////子栏目
function childNodeHtml(data,size,node,tcid,end,checked,bindMethod,mn){
	if(mn == undefined)
		mn = '';
	var childchildNode;
	//alert(tcid);
	itmeHtml += '<ul id="node'+mn+tcid+'" class="ul_tree_node" style="display:block;">';
	$.each(data,function(i,u){
		
		if(u.tColumn.fcolumngp == tcid && u.tColumn.id != tcid){
			size --;
			childchildNode =  checkNode(data,u.tColumn.id);
						
			var	marginleft = eval((u.tColumn.fcolumnlevel - 1)  * 16);
			
			if(size > 0){
				if(childchildNode > 0){//子栏目下还有子栏目
					if(checked){
						itmeHtml += '<li  value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')"  value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					//itmeHtml += '<li  value="'+u.id+'"><img src="'+node+'/images/tree/elbow-line.gif" /><img onclick="openTree('+u.id+',\''+node+'\',false)" id="img'+u.id+'" src="'+node+'/images/tree/elbow-minus.gif"  style="margin-left:'+marginleft+'px;"/><img id="imgFold'+u.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
					childNodeHtml(data,childchildNode,node,u.tColumn.id,false,checked,mn);
				}else{
					if(checked){
						itmeHtml += '<li  value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+ eval(marginleft + 20)+'px;"/>'+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')" value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+ eval(marginleft + 20)+'px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}
			}else{
				if(childchildNode > 0){//子栏目下还有子栏目
					if(checked){
						itmeHtml += '<li value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')" value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					childNodeHtml(data,childchildNode,node,u.tColumn.id,true,checked,mn);
				}else{
					if(checked){
						itmeHtml += '<li value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+eval(marginleft + 20)+'px;"/>'+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this,\'' + bindMethod + '\')" value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+eval(marginleft + 20)+'px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}
			}
		}
	});
	itmeHtml += '</ul>';
	return itmeHtml;
}
///节点开关控制
function openTree(uid,node,mn){
	if(mn == undefined)
		mn = '';
	//alert(uid + mn);
	var disp = document.getElementById('node'+mn+uid);
	if(disp.style.display == 'block'){
		disp.style.display = 'none';
		document.getElementById('img'+mn+uid).src = node + '/images/tree/node+.png';
		document.getElementById('imgFold'+mn+uid).src = node + '/images/tree/folder.gif';
	}else{
		disp.style.display = 'block';
		document.getElementById('img'+mn+uid).src = node + '/images/tree/node-.png';
		document.getElementById('imgFold'+mn+uid).src = node + '/images/tree/folder-open.gif';
	}
}
/***
 * 根据栏目查询
 * @param obj
 */
var liObj;
function selectTree(obj,bindMethod){
	liObj = obj;
	
	$(".ul_tree").find("li").removeClass();
	obj.className = 'on_li';
	eval(bindMethod);
}